package com.wondertek.mist.plugins;

import org.apache.cordova.CordovaArgs;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;

import com.wondertek.mist.utils.BitandBaseUtil;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class CropPhotoPlugin  extends CordovaPlugin{

	private Uri imageUri;

	private CallbackContext callbackContext;



	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {

		imageUri=null;

		this.callbackContext=callbackContext;

		if("getCameraRoll".equalsIgnoreCase(action)){
			imageUri = Uri.parse("file:///sdcard/"+System.currentTimeMillis()+".jpg");
			Runnable runnable=new Runnable(){
			
				public void run(){
					Intent intent = new Intent(Intent.ACTION_PICK, null);
					intent.setType("image/*");
					intent.putExtra("crop", "true");
					//intent.putExtra("aspectX", 1);
					//intent.putExtra("aspectY", 1);
					intent.putExtra("outputX", 700);
					intent.putExtra("outputY", 700);
					intent.putExtra("scale", true);
					intent.putExtra("return-data", false);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
					intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
					intent.putExtra("noFaceDetection", true); // no face detection
					Log.i("getPath", imageUri.getPath());
					CropPhotoPlugin.this.cordova.startActivityForResult(CropPhotoPlugin.this, intent, 1);
				}
			};
			CropPhotoPlugin.this.cordova.getActivity().runOnUiThread(runnable);

		}else if("getCameraImage".equalsIgnoreCase(action)){

			JSONObject message = (JSONObject)args.getJSONObject(0);

			String imageType = message.getString("imageType");
			this.imageUri = Uri.parse(imageType);

			Runnable runnable=new Runnable(){
				public void run(){
					Intent intent = new Intent("com.android.camera.action.CROP");
					intent.setDataAndType(imageUri, "image/*");
					intent.putExtra("crop", "true");
					//intent.putExtra("aspectX", 1);
					//intent.putExtra("aspectY", 1);
					intent.putExtra("outputX", 700);
					intent.putExtra("outputY", 700);
					intent.putExtra("scale", true);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
					intent.putExtra("return-data", false);
					intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
					intent.putExtra("noFaceDetection", true); // no face detection
					CropPhotoPlugin.this.cordova.startActivityForResult(CropPhotoPlugin.this, intent,2);
				}
			};
			CropPhotoPlugin.this.cordova.getActivity().runOnUiThread(runnable);
		}
		PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
		r.setKeepCallback(true);
		callbackContext.sendPluginResult(r);
		return true;
	}



	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 1:
			
		case 2:
			if(data!=null){
				Bitmap photo=BitmapFactory.decodeFile(imageUri.getPath());
				Bitmap smallPhoto=Bitmap.createScaledBitmap(photo, 40, 40, true);
				String base64 = BitandBaseUtil.bitmapToBase64(smallPhoto);
				JSONObject jsonObject = new JSONObject();
				try {
					jsonObject.put("base64", base64);
					jsonObject.put("tmpFile", imageUri.getPath());
					Log.i("JSON", jsonObject.toString());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,imageUri.getPath()));
			}

			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
